package util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class ConnectionFactory {

	private static Properties props;

	static {
		props = new Properties();
		//获取流
		try {
			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("xxx.properties");
			//
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException {
		//1.注册驱动
		try {
			Class.forName(props.getProperty("driverClassName"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//2.获取连接
		String url = props.getProperty("url");
		String user = props.getProperty("username");
		String pwd = props.getProperty("password");
		return DriverManager.getConnection(url, user, pwd);
	}

	public static void main(String[] args) throws SQLException {
		System.out.println(getConnection());
	}
}